import UIAbility from '@ohos.app.ability.UIAbility';
import hilog from '@ohos.hilog';
import window from '@ohos.window';
import backgroundTaskManager from '@ohos.resourceschedule.backgroundTaskManager';
import wantAgent from '@ohos.app.ability.wantAgent';

export default class UDPEchoServerAbility extends UIAbility {
  onCreate(want, launchParam) {
    // let wantAgentInfo = {
    //   wants: [
    //     {
    //       bundleName: "com.zl.network.demo.udp.echoserver",
    //       abilityName: "UDPEchoServerAbility"
    //     }
    //   ],
    //   operationType: wantAgent.OperationType.START_ABILITY,
    //   requestCode: 0,
    //   wantAgentFlags: [wantAgent.WantAgentFlags.UPDATE_PRESENT_FLAG]
    // };
    //
    // wantAgent.getWantAgent(wantAgentInfo).then((wantAgentObj) => {
    //   try {
    //     backgroundTaskManager.startBackgroundRunning(this.context,
    //       backgroundTaskManager.BackgroundMode.DATA_TRANSFER, wantAgentObj).then(() => {
    //       console.info("Operation startBackgroundRunning succeeded");
    //     }).catch((error) => {
    //       console.error(`Operation startBackgroundRunning failed. code is ${error.code} message is ${error.message}`);
    //     });
    //   } catch (error) {
    //     console.error(`Operation startBackgroundRunning failed. code is ${error.code} message is ${error.message}`);
    //   }
    // });

  }

  onDestroy() {
    // try {
    //   backgroundTaskManager.stopBackgroundRunning(this.context).then(() => {
    //     console.info("Operation stopBackgroundRunning succeeded");
    //   }).catch((error) => {
    //     console.error(`Operation stopBackgroundRunning failed. code is ${error.code} message is ${error.message}`);
    //   });
    // } catch (error) {
    //   console.error(`Operation stopBackgroundRunning failed. code is ${error.code} message is ${error.message}`);
    // }
  }

  onWindowStageCreate(windowStage: window.WindowStage) {
    // Main window is created, set main page for this ability
    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate');

    windowStage.loadContent('pages/Index')
  }

  onWindowStageDestroy() {
    // Main window is destroyed, release UI related resources
    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageDestroy');
  }

  onForeground() {
    // Ability has brought to foreground
    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onForeground');
  }

  onBackground() {
    // Ability has back to background
    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onBackground');
  }
}
